#include "LedView.h"
#include <math.h>
#include "ScreenPresentation.h"
#include <QGraphicsSceneMouseEvent>

LedView::LedView() :
	_x(0),_y(0),_radius(0),
	_startAngle(0),_angleWidth(0),
	_parent(NULL),
	_hover(false),
	_active(true),
	_mainWidget(NULL),
	_headLed(false),
	_selectable(false)
{
}

LedView::LedView(int x, int y, int radius, int startAngle, int angleWidth, QWidget* parent) :
	_x(x),_y(y),_radius(radius),
	_startAngle(startAngle),_angleWidth(angleWidth),
	_parent(parent),
	_hover(false),
	_active(true),
	_mainWidget(NULL),
	_headLed(false),
	_selectable(false)
{
	setAcceptHoverEvents(true);
	setPos(0,0);

	int angle = (_startAngle + _angleWidth/2 + 180) %360;
	_pointCenter = QPoint(_x + _radius/3*cos(angle*M_PI/180), _y - _radius/3*sin(angle*M_PI/180));

}

void LedView::setMainWidget(ScreenPresentation* screen)
{
	_mainWidget = screen;
}

void LedView::setPosition(int x, int y){
	_x = x;
	_y = y;
	setPos(0,0);
	int angle = (_startAngle + _angleWidth/2 + 180) %360;
	_pointCenter = QPoint(_x + _radius/3*cos(angle*M_PI/180), _y - _radius/3*sin(angle*M_PI/180));
}

void LedView::setDirection(int angle){
	_startAngle = angle - _angleWidth/2;
}

void LedView::setAngleWidth(int width){
	_angleWidth = width;
}

void LedView::setColor(QColor col){
	_col = col;
}

void LedView::setRadius(int radius){
	_radius = radius;
}

bool LedView::isLedActive()
{
	return _active;
}

QRectF LedView::boundingRect() const{
	QRectF rect = QRectF(_x-_radius, _y-_radius, _radius*2, _radius*2);
	int angle = (_startAngle + _angleWidth/2 + 180) %360;

	if(angle == 180) rect.setLeft(_x - _radius/3 - _radius/8);
	if(angle == 270) rect.setBottom(_y + _radius/3 + _radius/8);
	if(angle == 0) rect.setRight(_x + _radius/3 + _radius/8);
	if(angle == 90) rect.setTop(_y - _radius/3 - _radius/8);
	return rect;
}

QPainterPath LedView::shape() const{
	QPainterPath myPath;
	if(_active){
		myPath.moveTo(_x,_y);
		myPath.arcTo(_x-_radius, _y-_radius, _radius*2, _radius*2, _startAngle, _angleWidth);
		myPath.closeSubpath();
	}

	myPath.addEllipse(_pointCenter,_radius/10,_radius/10);

	return myPath;
}

void LedView::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){

	if(_active){
		drawLight(painter);
	}

	if(!_selectable) return;

	drawPoint(painter);

	if(_hover){
		if(_active) drawLightSelection(painter);
		drawPointSelection(painter);
	}
}

void LedView::drawLight(QPainter* painter){
	QPen penOrigin = painter->pen();
	painter->setPen(Qt::NoPen);

	QBrush brushOrigin = painter->brush();
	QRadialGradient gradient(_x, _y, _radius, _x, _y);
	gradient.setColorAt(0, QColor::fromRgb(_col.red(), _col.green(), _col.blue(), 255));
	gradient.setColorAt(1, QColor::fromRgb(255, 255, 255, 0));
	painter->setBrush(QBrush(gradient));

	QRectF rectangle(_x-_radius, _y-_radius, _radius*2, _radius*2);
	painter->drawPie(rectangle, _startAngle*16, _angleWidth*16);

	painter->setPen(penOrigin);
	painter->setBrush(brushOrigin);
}

void LedView::drawLightSelection(QPainter* painter){
	QPen penOrigin = painter->pen();
	QBrush brushOrigin = painter->brush();

	painter->setPen(QColor(10,200,10));
	QPainterPath myPath;
	myPath.moveTo(_x,_y);
	myPath.arcTo(_x-_radius-1, _y-_radius-1, _radius*2+2, _radius*2+2, _startAngle, _angleWidth);
	myPath.closeSubpath();
	painter->drawPath(myPath);

	painter->setPen(penOrigin);
	painter->setBrush(brushOrigin);
}

void LedView::drawPointSelection(QPainter* painter){
	QPen penOrigin = painter->pen();
	QBrush brushOrigin = painter->brush();

	painter->setPen(_active ? QColor(200,10,10) : QColor(10,200,10));
	painter->drawEllipse(_pointCenter,_radius/10+1,_radius/10+1);

	painter->setPen(penOrigin);
	painter->setBrush(brushOrigin);
}

void LedView::drawPoint(QPainter* painter){
	QPen penOrigin = painter->pen();
	QBrush brushOrigin = painter->brush();

	if(_active){
		painter->setPen(QColor(10,200,10));
		QBrush brush(QColor(10,200,10));
		painter->setBrush(brush);
		painter->drawEllipse(_pointCenter,_radius/10,_radius/10);
	}
	else{
		painter->setPen(QColor(200,10,10));
		QBrush brush(QColor(200,10,10));
		painter->setBrush(brush);
		painter->drawEllipse(_pointCenter,_radius/10,_radius/10);
	}

	painter->setPen(penOrigin);
	painter->setBrush(brushOrigin);
}

void LedView::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
	if(!_selectable) return;
	_hover = true;
	setZValue(1000);
	update();
}

void LedView::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
	if(!_selectable) return;
	_hover = false;
	setZValue(100);
	update();
}

void LedView::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
	if(!_selectable) return;
	if(event->buttons() & Qt::LeftButton){
		_active = !_active;
		setZValue(_active ? 100 : 99);
		update();
		if(_mainWidget) _mainWidget->updateStartLed();
	}
	if(event->buttons() & Qt::RightButton){
		if(_mainWidget){
			_mainWidget->setStartLed(this);
			_mainWidget->updateStartLed();
		}
	}
}

void LedView::setStart(bool start)
{
	_headLed = start;
	update();
}

